package com.ice_col.viewrain.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;

import com.ice_col.viewrain.R;
import com.ice_col.viewrain.entity.RainDrop;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/9/9 0009.
 */
    public class RainView extends BaseView{

        private static Paint mPaint;
        private List<RainDrop> mRainDrops;
        private int mHeight;
        private int mWidth;
        private int rainNum;



        public RainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.RainView);
        rainNum=typedArray.getInteger(R.styleable.RainView_raindropNum,80);
        mPaint=new Paint();
        mRainDrops=new ArrayList<>();
        mPaint.setColor(Color.WHITE);
        mHeight=800;
        mWidth=480;
        initRain();
    }

    private void initRain(){
        for(int i=0;i<rainNum;i++){
            mRainDrops.add(new RainDrop(mWidth,mHeight));
        }
    }
    public RainView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public RainView(Context context) {
        super(context);

    }

    @Override
    protected void myOnDraw(Canvas canvas) {
        for (RainDrop rainDrop:mRainDrops) {
            rainDrop.drawSelf(canvas,mPaint);
        }
    }

    @Override
    protected void reDraw() {
        for (RainDrop rainDrop:mRainDrops) {
            rainDrop.drop();
            if(rainDrop.getmStartX()>=mWidth||rainDrop.getmStartY()>=mHeight){
              rainDrop.init();
            }
        }
    }

}
